package com.feicheng.service.impl;

import com.feicheng.common.constant.Constant;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.message.WxMessage;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.persistence.entity.Client;
import com.feicheng.persistence.service.IClientService;
import com.feicheng.service.IFWxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;

@Service
public class FWxServiceImpl implements IFWxService {

    @Autowired
    IClientService clientService;

    @Autowired
    HttpSession session;

    @Autowired
    WxMessage wxMessage;

    @Override
    public ResultInfo autorBind(String tel, String password, String openId, Boolean sex, Integer age, String name) {

        if (!StringUtils.hasLength(openId)) {
            throw new SystemException(wxMessage.getE01());
        }

        Client newAutor = new Client();
        newAutor.setOpenId(openId);

        if (StringUtils.hasLength(name)) {
            newAutor.setName(name);
        }

        if (null != age) {
            newAutor.setAge(age);
        }

        if (null != sex) {
            newAutor.setSex(sex);
        }

        clientService.save(newAutor);
        session.setAttribute(Constant.RESULT.CURRENT_CLIENT, newAutor);
        return ResultInfo.success(newAutor);

    }
}
